/**
 * n-grid
 * author: 'byhgj'
 * git: https://gitee.com/byhgj/n-popup.git
 */
class nPopup {
	constructor(opts){
		if (!opts.el) {console.log('need el'); return}
		this.opts = Object.assign({
			visible: false,
			disableBrowerContextMenu: true
		}, opts)
		this.menu = document.querySelector(opts.el);
		this.menu.style.position = 'absolute';
		this.menu.style.display = this.opts.visible ? '' : 'none';
		document.querySelectorAll('*').forEach( c => c.addEventListener('click', () => {this.hide()}));
		if (this.opts.disableBrowerContextMenu) document.oncontextmenu = function(){return false}
	}
	show(x, y){
		this.menu.style.left = (x || window.event.clientX) + 'px';
		this.menu.style.top = (y || window.event.clientY) + 'px';
		this.menu.style.display = '';
	}
	hide(){
		this.menu.style.display = 'none';		
	}
}

(function(){
	window.npopup = function(opts){
		return new nPopup(opts);
	}
})();